<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="padding: 10px 0">
              <el-button icon="el-icon-plus" size="small" type="primary" @click.native="dialogVisible = true">
                新增角色
              </el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="rowsDate">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作" :data="rowsDate">
                <template #default="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary">编辑</el-button>
                  <el-button size="small" type="danger" @click.native="delUser(row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="prev, pager, next, sizes"
                :page-sizes="[2, 4, 5, 8]"
                background
                :total="pagingData.total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增弹层 -->
    <template>
      <el-dialog title="提示" width="50%" :visible.sync="dialogVisible" @close="close">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="newlyAdded">确 定</el-button>
        </span>
      </el-dialog>
    </template>
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole } from '@/api/setting.js'
export default {
  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '角色名称长度2-10', trigger: 'blur' }
        ],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      dialogVisible: null,
      pagingData: {
        page: 1, // 页码
        pagesize: 2, // 每页显示多少条数据
        total: 0 // 总数据
      },
      rowsDate: []
    }
  },
  created() {
    this.getUserData()
  },
  methods: {
    close() {
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除校验
      this.$refs.roleForm.resetFields()
      // 关闭弹框
      this.showDialog = false
    },
    // 新增
    async newlyAdded() {
      try {
        await this.$refs.roleForm.validate()
        await addRole(this.roleForm)
        this.pagingData.page = 1
        this.dialogVisible = false
        this.$message.success('添加成功')
        this.getUserData()
      } catch (error) {
        console.log(error)
      }
    },
    // 删除
    async delUser(val) {
      try {
        await this.$confirm(`你是否要删除${val.name}?`)
        deleteRole(val.id)
        this.getUserData()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    async getUserData() {
      const paging = await getRoleList(this.pagingData)
      // console.log(paging)
      this.pagingData.total = paging.total
      this.rowsDate = paging.rows
    },
    handleSizeChange(val) {
      // console.log('handleSizeChange')
      this.pagingData.pagesize = val
      this.getUserData()
    },
    handleCurrentChange(val) {
      // console.log('handleCurrentChange', val)
      this.pagingData.page = val
      this.getUserData()
    }
  }
}
</script>

<style></style>
